home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / util / edit / aux_emacs.lha / termio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-18  |  6.8 KB  |  316 lines

  1. /*
  2.  * The functions in this file negotiate with the operating system for
  3.  * characters, and write characters in a barely buffered fashion on the display.
  4.  * All operating systems.
  5.  */
  6. #include    <stdio.h>
  7. #include    "ed.h"
  8.  
  9. #ifdef    AMIGA
  10. #define NEW 1006
  11. #define    AMG_MAXBUF    1024
  12. static long terminal;
  13. static char    scrn_tmp[AMG_MAXBUF+1];
  14. static int    scrn_tmp_p = 0;
  15. #endif
  16.  
  17. #ifdef    VMS
  18. #include    <stsdef.h>
  19. #include    <ssdef.h>
  20. #include    <descrip.h>
  21. #include    <iodef.h>
  22. #include    <ttdef.h>
  23.  
  24. #define NIBUF    128            /* Input buffer size        */
  25. #define NOBUF    1024            /* MM says bug buffers win!    */
  26. #define EFN    0            /* Event flag            */
  27.  
  28. char    obuf[NOBUF];            /* Output buffer        */
  29. int    nobuf;            /* # of bytes in above      */
  30. char    ibuf[NIBUF];            /* Input buffer         */
  31. int    nibuf;            /* # of bytes in above    */
  32. int    ibufi;            /* Read index            */
  33. int    oldmode[2];            /* Old TTY mode bits        */
  34. int    newmode[2];            /* New TTY mode bits        */
  35. short    iochan;             /* TTY I/O channel        */
  36. #endif
  37.  
  38. #ifdef    CPM
  39. #include    <bdos.h>
  40. #endif
  41.  
  42. #if    MSDOS
  43. #undef    LATTICE
  44. #include    <dos.h>
  45. #endif
  46.  
  47. #if RAINBOW
  48. #include "rainbow.h"
  49. #endif
  50.  
  51. #ifdef V7
  52. #include    <sgtty.h>        /* for stty/gtty functions */
  53. struct  sgttyb  ostate;         /* saved tty state */
  54. struct  sgttyb  nstate;         /* values for editor mode */
  55. #endif
  56.  
  57. /*
  58.  * This function is called once to set up the terminal device streams.
  59.  * On VMS, it translates SYS$INPUT until it finds the terminal, then assigns
  60.  * a channel to it and sets it raw. On CPM it is a no-op.
  61.  */
  62. ttopen()
  63. {
  64. #ifdef    AMIGA
  65.     extern int Enable_Abort;
  66.  
  67.     Enable_Abort = 0;
  68.     if (IsInteractive(Input()) && IsInteractive(Output()))
  69.        {
  70.        terminal = Open("Aux:set/e:off/c:off/r:on", NEW); 
  71.            Close(terminal);            /* Just set paramters */
  72.        terminal = (long) Output();
  73.        /* set_raw(); */
  74.        }
  75.     else
  76.     terminal = Open("RAW:0/0/639/199/MicroEmacs", NEW);
  77. #endif
  78. #ifdef    VMS
  79.     struct  dsc$descriptor  idsc;
  80.     struct  dsc$descriptor  odsc;
  81.     char    oname[40];
  82.     int    iosb[2];
  83.     int    status;
  84.  
  85.     odsc.dsc$a_pointer = "SYS$INPUT";
  86.     odsc.dsc$w_length  = strlen(odsc.dsc$a_pointer);
  87.     odsc.dsc$b_dtype    = DSC$K_DTYPE_T;
  88.     odsc.dsc$b_class    = DSC$K_CLASS_S;
  89.     idsc.dsc$b_dtype    = DSC$K_DTYPE_T;
  90.     idsc.dsc$b_class    = DSC$K_CLASS_S;
  91.     do {
  92.         idsc.dsc$a_pointer = odsc.dsc$a_pointer;
  93.         idsc.dsc$w_length  = odsc.dsc$w_length;
  94.         odsc.dsc$a_pointer = &oname[0];
  95.         odsc.dsc$w_length  = sizeof(oname);
  96.         status = LIB$SYS_TRNLOG(&idsc, &odsc.dsc$w_length, &odsc);
  97.         if (status!=SS$_NORMAL && status!=SS$_NOTRAN)
  98.             exit(status);
  99.         if (oname[0] == 0x1B) {
  100.             odsc.dsc$a_pointer += 4;
  101.             odsc.dsc$w_length  -= 4;
  102.         }
  103.     } while (status == SS$_NORMAL);
  104.     status = SYS$ASSIGN(&odsc, &iochan, 0, 0);
  105.     if (status != SS$_NORMAL)
  106.         exit(status);
  107.     status = SYS$QIOW(EFN, iochan, IO$_SENSEMODE, iosb, 0, 0,
  108.               oldmode, sizeof(oldmode), 0, 0, 0, 0);
  109.     if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL)
  110.         exit(status);
  111.     newmode[0] = oldmode[0];
  112.     newmode[1] = oldmode[1] | TT$M_PASSALL | TT$M_NOECHO;
  113.     status = SYS$QIOW(EFN, iochan, IO$_SETMODE, iosb, 0, 0,
  114.               newmode, sizeof(newmode), 0, 0, 0, 0);
  115.     if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL)
  116.         exit(status);
  117. #endif
  118. #ifdef    CPM
  119. #endif
  120. #if    MSDOS
  121. #endif
  122. #ifdef    V7
  123.     gtty(1, &ostate);            /* save old state */
  124.     gtty(1, &nstate);            /* get base of new state */
  125.     nstate.sg_flags |= RAW;
  126.     nstate.sg_flags &= ~(ECHO|CRMOD);    /* no echo for now... */
  127.     stty(1, &nstate);            /* set mode */
  128. #endif
  129. }
  130.  
  131. /*
  132.  * This function gets called just before we go back home to the command
  133.  * interpreter. On VMS it puts the terminal back in a reasonable state.
  134.  * Another no-operation on CPM.
  135.  */
  136. ttclose()
  137. {
  138. #ifdef    AMIGA
  139.     amg_flush();
  140.     if (Output() != terminal) Close(terminal); /* else set_con(); */
  141.         else
  142.            {
  143.            terminal = Open("Aux:set/c:on/e:on/r:off", NEW);  /* reset paramters */
  144.            Close(terminal);
  145.            }
  146. #endif
  147. #ifdef    VMS
  148.     int    status;
  149.     int    iosb[1];
  150.  
  151.     ttflush();
  152.     status = SYS$QIOW(EFN, iochan, IO$_SETMODE, iosb, 0, 0,
  153.          oldmode, sizeof(oldmode), 0, 0, 0, 0);
  154.     if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL)
  155.         exit(status);
  156.     status = SYS$DASSGN(iochan);
  157.     if (status != SS$_NORMAL)
  158.         exit(status);
  159. #endif
  160. #ifdef    CPM
  161. #endif
  162. #if    MSDOS
  163. #endif
  164. #ifdef    V7
  165.     stty(1, &ostate);
  166. #endif
  167. }
  168.  
  169. /*
  170.  * Write a character to the display. On VMS, terminal output is buffered, and
  171.  * we just put the characters in the big array, after checking for overflow.
  172.  * On CPM terminal I/O unbuffered, so we just write the byte out. Ditto on
  173.  * MS-DOS (use the very very raw console output routine).
  174.  */
  175. ttputc(c)
  176. #ifdef    AMIGA
  177.     char c;
  178. #endif
  179. {
  180. #ifdef    AMIGA
  181.     scrn_tmp[scrn_tmp_p++] = c;
  182.     if(scrn_tmp_p>=AMG_MAXBUF)
  183.         amg_flush();
  184. #endif
  185. #ifdef    VMS
  186.     if (nobuf >= NOBUF)
  187.         ttflush();
  188.     obuf[nobuf++] = c;
  189. #endif
  190.  
  191. #ifdef    CPM
  192.     bios(BCONOUT, c, 0);
  193. #endif
  194.  
  195. /* line below previously read MSDOS & CWC86  */
  196. #if    MSDOS
  197.     dosb(CONDIO, c, 0);
  198. #endif
  199.  
  200. #if RAINBOW
  201.     Put_Char(c);            /* fast video */
  202. #endif
  203.  
  204. #ifdef    V7
  205.     fputc(c, stdout);
  206. #endif
  207. }
  208.  
  209. amg_flush()
  210. {
  211.     if(scrn_tmp_p)
  212.         Write(terminal,scrn_tmp,scrn_tmp_p);
  213.     scrn_tmp_p = 0;
  214. }
  215.  
  216. /*
  217.  * Flush terminal buffer. Does real work where the terminal output is buffered
  218.  * up. A no-operation on systems where byte at a time terminal I/O is done.
  219.  */
  220. ttflush()
  221. {
  222. #ifdef    AMIGA
  223.     amg_flush();
  224. #endif
  225. #ifdef    VMS
  226.     int    status;
  227.     int    iosb[2];
  228.  
  229.     status = SS$_NORMAL;
  230.     if (nobuf != 0) {
  231.         status = SYS$QIOW(EFN, iochan, IO$_WRITELBLK|IO$M_NOFORMAT,
  232.              iosb, 0, 0, obuf, nobuf, 0, 0, 0, 0);
  233.         if (status == SS$_NORMAL)
  234.             status = iosb[0] & 0xFFFF;
  235.         nobuf = 0;
  236.     }
  237.     return (status);
  238. #endif
  239. #ifdef    CPM
  240. #endif
  241. #if    MSDOS
  242. #endif
  243. #ifdef    V7
  244.     fflush(stdout);
  245. #endif
  246. }
  247.  
  248. /*
  249.  * Read a character from the terminal, performing no editing and doing no echo
  250.  * at all. More complex in VMS that almost anyplace else, which figures. Very
  251.  * simple on CPM, because the system can do exactly what you want.
  252.  */
  253. ttgetc()
  254. {
  255. #ifdef    AMIGA
  256.     char ch;
  257.     amg_flush();
  258.     Read(terminal, &ch, 1);
  259.     return (int) ch;
  260. #endif
  261. #ifdef    VMS
  262.     int    status;
  263.     int    iosb[2];
  264.     int    term[2];
  265.  
  266.     while (ibufi >= nibuf) {
  267.         ibufi = 0;
  268.         term[0] = 0;
  269.         term[1] = 0;
  270.         status = SYS$QIOW(EFN, iochan, IO$_READLBLK|IO$M_TIMED,
  271.              iosb, 0, 0, ibuf, NIBUF, 0, term, 0, 0);
  272.         if (status != SS$_NORMAL)
  273.             exit(status);
  274.         status = iosb[0] & 0xFFFF;
  275.         if (status!=SS$_NORMAL && status!=SS$_TIMEOUT)
  276.             exit(status);
  277.         nibuf = (iosb[0]>>16) + (iosb[1]>>16);
  278.         if (nibuf == 0) {
  279.             status = SYS$QIOW(EFN, iochan, IO$_READLBLK,
  280.                  iosb, 0, 0, ibuf, 1, 0, term, 0, 0);
  281.             if (status != SS$_NORMAL
  282.             || (status = (iosb[0]&0xFFFF)) != SS$_NORMAL)
  283.                 exit(status);
  284.             nibuf = (iosb[0]>>16) + (iosb[1]>>16);
  285.         }
  286.     }
  287.     return (ibuf[ibufi++] & 0xFF);      /* Allow multinational  */
  288. #endif
  289.  
  290. #ifdef    CPM
  291.     return (biosb(BCONIN, 0, 0));
  292. #endif
  293.  
  294. #if RAINBOW
  295.     int Ch;
  296.  
  297.     while ((Ch = Read_Keyboard()) < 0);
  298.  
  299.     if ((Ch & Function_Key) == 0)
  300.         if (!((Ch & 0xFF) == 015 || (Ch & 0xFF) == 0177))
  301.             Ch &= 0xFF;
  302.  
  303.     return Ch;
  304. #endif
  305.  
  306. #if    MSDOS
  307.     return (dosb(CONRAW, 0, 0));
  308. #endif
  309.  
  310. #ifdef    V7
  311.     return(fgetc(stdin));
  312. #endif
  313. }
  314.  
  315.  
  316.